﻿using Dapper;
using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using Project.Infrastructure.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ProjectEntity = Project.Domain.AggregateModels.Project;

namespace Project.API.Application.Queries
{
    public class ProjectQueries
        : IProjectQueries
    {
        private readonly ProjectContext _context;
        public ProjectQueries(ProjectContext context)
        {
            _context = context;
        }
        public async Task<Domain.AggregateModels.Project> GetProjectDetailAsync(int projectId)
        {
            using (var myConn = _context.Database.GetDbConnection())
            {
                myConn.Open();
                var strSql = "select * from Project where Id=@Id";
               return await myConn.QueryFirstOrDefaultAsync<ProjectEntity>(strSql,projectId);
            }
        }

        public async Task<List<Domain.AggregateModels.Project>> GetProjectsByUserIdAsync(int userId)
        {
            using (var myConn = _context.Database.GetDbConnection())
            {
                myConn.Open();
                var strSql = "select * from Project where UserId=@UserId";
                return (await myConn.QueryAsync<ProjectEntity>(strSql, new { UserId=userId})).ToList();
            }
        }
    }
}
